<template>
  <div class="HomePage">
    <div class="home">
      <div style="display: flex; justify-content: center">
        <div class="headerr">
          <h1>欢迎来到 ZJHUOJ 在线编程平台</h1>
          <p class="subtitle">让我们一起探索编程的无限可能！</p>
        </div>
      </div>
      <div class="content">
        <div class="left">
          <!-- 系统公告 -->
          <div class="section">
            <h3 class="section-title">系统公告</h3>
            <p>暂无最新公告，敬请关注！</p>
          </div>

          <!-- 统计信息 -->
          <div class="section">
            <h3 class="section-title">统计信息</h3>
            <ul>
              <li>题目总数：{{ totalProblems }}</li>
              <li>用户总数：{{ totalUsers }}</li>
              <li>总提交：{{ totalSubmissions }}</li>
            </ul>
          </div>

          <!-- 用户排名 -->
          <div class="section">
            <h3 class="section-title">总排名</h3>
            <ul>
              <li v-for="(user, index) in topUsers" :key="user.name">
                {{ index + 1 }}. {{ user.username }}
              </li>
            </ul>
          </div>
        </div>

        <div class="right">
          <!-- 平台简介 -->
          <div class="section">
            <h3 class="section-title">平台简介</h3>
            <p>我们提供丰富的题库，便于开发者练习并提升技能。</p>
          </div>
          <!-- 竞赛信息 -->
          <div class="section">
            <h3 class="section-title">竞赛信息</h3>
            <p>当前没有竞赛进行，敬请期待下次挑战！</p>
          </div>

          <!-- 最近新题目 -->
          <div class="section">
            <h3 class="section-title">最近新题目</h3>
            <ul>
              <li v-for="problem in latestProblems" :key="problem.id">
                题目ID {{ problem.id }} - {{ problem.title }}
              </li>
            </ul>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import request from "@/utils/request"; // 引入请求工具

export default {
  name: "HomePage",
  data() {
    return {
      totalProblems: 0, // 题目总数
      totalUsers: 0, // 用户总数
      totalSubmissions: 0, // 总提交数
      topUsers: [], // 排名前三的用户
      latestProblems: [], // 最近新题目
    };
  },
  mounted() {
    // 获取所有用户、题目和提交信息
    this.fetchUserData();
    this.fetchProblemData();
    this.fetchSubmissionData();
  },
  methods: {
    // 获取用户数据
    async fetchUserData() {
      try {
        const response = await request.get("/user/selectAll");
        const users = response.data;
        this.totalUsers = users.length;
        this.topUsers = users
          .sort((a, b) => {
            // 按照解决数降序排序，解决数相同再按提交数升序排序
            if (b.solved === a.solved) {
              return a.submissions - b.submissions; // 提交数少的排前面
            }
            return b.solved - a.solved; // 解决数多的排前面
          })
          .slice(0, 10); // 获取前三名
      } catch (error) {
        console.error("获取用户数据失败", error);
      }
    },

    // 获取题目数据
    async fetchProblemData() {
      try {
        const response = await request.get("/problem/selectAll");
        const problems = response.data;
        this.totalProblems = problems.length;
        this.latestProblems = problems.slice(
          this.totalProblems - 15,
          this.totalProblems
        ); // 获取前15个最新题目
      } catch (error) {
        console.error("获取题目数据失败", error);
      }
    },

    // 获取总提交数
    async fetchSubmissionData() { 
      try {
        const response = await request.get(`/submission/getAll/35`, {
          params: { pageNum: 1, pageSize: 9999 },
        });
        this.totalSubmissions = response.totalPages; // 获取总页数
      } catch (error) {
        console.error("获取提交数据失败", error);
      }
    },
  },
};
</script>

<style scoped>
/* 主背景和字体 */
.HomePage {
  width: 80%;
  margin: 30px auto;
  box-sizing: border-box;
}
.home {
  background-color: #f4f5f7;
  color: #333;
  font-family: "Arial", sans-serif;
  padding: 0;
  height: 990px;
  display: flex;
  flex-direction: column;
}

/* 页面标题样式 */
.headerr {
  width: 96%;
  text-align: center;
  padding: 20px 0;
  background: linear-gradient(135deg, #cecece, #dfdfdf); /* 渐变背景 */
  border-bottom: 1px solid #ccc;
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1); /* 阴影效果 */
  border-radius: 10px;
}

.headerr h1 {
  font-size: 50px;
  font-weight: bold;
  color: #fff;
  letter-spacing: 2px; /* 字间距调整 */
  margin-bottom: 10px;
  animation: fadeIn 1s ease-out; /* 添加淡入动画 */
}

.headerr .subtitle {
  font-size: 20px;
  color: #f8f8f8;
  margin-top: 10px;
  animation: fadeIn 1.5s ease-out; /* 副标题的淡入动画 */
}

/* 动画效果 */
@keyframes fadeIn {
  0% {
    opacity: 0;
  }
  100% {
    opacity: 1;
  }
}

/* 内容区域 */
.content {
  display: flex;
  justify-content: space-between;
  padding: 40px;
  flex: 1;
  gap: 20px;
}

.content .left,
.content .right {
  width: 960px;
  background-color: white;
  padding: 20px;
  border-radius: 10px;
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}

.section {
  margin-bottom: 30px;
}

.section-title {
  font-size: 20px;
  font-weight: bold;
  color: #444;
  border-bottom: 2px solid #e0e4e8;
  padding-bottom: 10px;
  margin-bottom: 10px;
}

.section p,
.section ul {
  font-size: 16px;
  color: #555;
  line-height: 1.8;
}

.section ul {
  list-style: none;
  padding-left: 0;
}

.section li {
  margin: 5px 0;
}
</style>
